<?php
/**
 * initialise currencies
 * see {@link  http://www.zen-cart.com/wiki/index.php/Developers_API_Tutorials#InitSystem wikitutorials} for more details.
 *
 * @package initSystem
 * @copyright Copyright 2003-2007 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: init_currencies.php 6300 2007-05-11 15:49:41Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}

 if(isset($_GET['language']))
 {
    switch($_GET['language'])
    {
        case 'ES':            
            $_SESSION['currency'] = 'EUR';
            $_SESSION['zonetwo'] = 'SPAIN';
            unset($_SESSION['zoneone']);            
            unset($_SESSION['zonethree']);
            break;
        case 'BR':
            $_SESSION['currency'] = 'BRL';
            $_SESSION['zoneone'] = 'BRASIL';
            unset($_SESSION['zonetwo']);            
            unset($_SESSION['zonethree']);
            break;
        case 'PT':
            $_SESSION['currency'] = 'EUR';
            $_SESSION['zonethree'] = 'PORTUGAL';
            unset($_SESSION['zonetwo']);            
            unset($_SESSION['zoneone']);
            break;
    }
 }
// If no currency is set, use appropriate default
if (!isset($_SESSION['currency']) && !isset($_GET['currency']) ) $_SESSION['currency'] = DEFAULT_CURRENCY;
$new_currency = false;
if(!isset($_SESSION['customer_id']))//check user logged in
{
    if(isset($_COOKIE['COOKIES_OF_COUNTRY_CODE']) && $_COOKIE['COOKIES_OF_COUNTRY_CODE'] !== '')
    {
        $country_code2 = $_COOKIE['COOKIES_OF_COUNTRY_CODE'];        
    }
    else
    {
        require_once(DIR_FS_CATALOG.'/includes/classes/ip2country.php5.php');
        $ip2c = new ip2country();
        $ip = $ip2c->get_client_ip();
        $ip2c->getInfo($ip);
        $country_code2 = $ip2c->get_country_code_2();
    }
    $new_currency = zen_currency_exists_follow_country_code2($country_code2);
}

if($new_currency === false || $new_currency === "")
{
    // Validate selected new currency, if any. Is false if valid not found.
    $new_currency = (isset($_GET['currency'])) ? zen_currency_exists($_GET['currency']) : zen_currency_exists($_SESSION['currency']);
}
// Validate language-currency and default-currency if relevant. Is false if valid not found.
if ($new_currency == false || isset($_GET['language'])) $new_currency = (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') ? zen_currency_exists(LANGUAGE_CURRENCY) : $new_currency;

// Final check -- if selected currency is bad and the "default" is bad, default to the first-found currency in order of exch rate.
if ($new_currency == false) $new_currency = zen_currency_exists(DEFAULT_CURRENCY, true);
//echo '<br />NEW = ' . $new_currency . '<br />';

// Now apply currency update
if (
   // Has new currency been selected?
  (isset($_GET['currency'])) || 

  // Does language change require currency update?
  (isset($_GET['language']) && USE_DEFAULT_LANGUAGE_CURRENCY == 'true' && LANGUAGE_CURRENCY != $_SESSION['currency']  ) ||
  $_SESSION['currency'] !== $new_currency
) {
  $_SESSION['currency'] = $new_currency;
  // redraw the page without the currency/language info in the URL
  //if (isset($_GET['currency']) || isset($_GET['language'])) zen_redirect(zen_href_link($current_page_base, zen_get_all_get_params(array('currency','language'))));
}
?>